/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.taskdefs;


import java.io.File;
import java.util.Set;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import org.emergent.antbite.savant.ArtifactTools;
import org.emergent.antbite.savant.LocalCacheStore;
import org.emergent.antbite.savant.SavantException;
import org.emergent.antbite.savant.ant.types.ArtifactType;
import org.emergent.antbite.savant.ant.types.DependenciesType;


/**
 * <p>
 * This class is an ant taskdef that publishes a local artifact
 * to the local cache store.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class PublishTask extends Task {

    private File from;
    private File localcache;
    private ArtifactType artifact;
    private DependenciesType deps;


    public File getFrom() {
        return from;
    }

    public void setFrom(File from) {
        this.from = from;
    }

    public File getLocalcache() {
        return localcache;
    }

    public void setLocalcache(File localcache) {
        this.localcache = localcache;
    }

    /**
     * Adds the artifact to the publish.
     *
     * @param   artifact The artifact
     * @throws  BuildException If the artifact was already set
     */
    public void addArtifact(ArtifactType artifact) {
        if (this.artifact != null) {
            throw new BuildException("Only one artifact per publish tag");
        }

        this.artifact = artifact;
    }

    /**
     * Adds the list of artifact dependencies. This will generate the XML dependency
     * file and publish it to the LocalCacheStore.
     *
     * @param   deps The dependencies.
     * @throws  BuildException If the dependencies were already set.
     */
    public void addDependencies(DependenciesType deps) {
        if (this.deps != null) {
            throw new BuildException("Only one artifact per publish tag");
        }

        this.deps = deps;
    }

    /**
     * Publishes the artifact specified from the file specified.
     *
     * @throws  BuildException If the publish fails
     */
    public void execute() throws BuildException {
        validate();

        LocalCacheStore lcs;
        try {
            lcs = new LocalCacheStore(localcache);
        } catch (SavantException se) {
            log("Invalid local cache location [" + localcache.getAbsolutePath() + "]");
            throw new BuildException(se);
        }

        try {
            lcs.store(artifact.getProxy(), from);
        } catch (SavantException se) {
            throw new BuildException(se);
        }

        log("Published artifact [" + artifact.getProxy() + "]");

        // Publish the XML file
        if (deps != null) {
            Set artifacts = deps.getAllArtifacts();
            if (artifacts.size() > 0) {
                File file = null;
                try {
                    file = ArtifactTools.generateXML(artifacts);
                    lcs.storeDeps(artifact.getProxy(), file);
                } catch (SavantException se) {
                    throw new BuildException(se);
                } finally {
                    if (file != null) {
                        file.delete();
                    }
                }
            }
        }
    }

    /**
     * Validates the parameters.
     */
    private void validate() {
        if (from == null) {
            throw new BuildException("from attribute required for publish task");
        }

        if (!from.exists() || from.isDirectory()) {
            throw new BuildException("Invalid from file [" + from.getAbsolutePath() +
                "]");
        }
    }
}